<?php

use Illuminate\Support\Arr;

if (!function_exists('user_admin_config')) {
    function user_admin_config($key = null, $value = null)
    {
        $session = session();

        if (!$config = $session->get('admin.config')) {
            $config = config('admin');

            $config['lang'] = config('app.locale');
        }

        if (is_array($key)) {
            // 保存
            foreach ($key as $k => $v) {
                Arr::set($config, $k, $v);
            }

            $session->put('admin.config', $config);

            return;
        }

        if ($key === null) {
            return $config;
        }

        return Arr::get($config, $key, $value);
    }
}

if (!function_exists('diffBetweenTwoDays')) {
    function diffBetweenTwoDays($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp     = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }
}

if (!function_exists('str_limit')) {
    function str_limit($str, $len, $new_str)
    {
        $str = mb_substr($str, 0, $len);

        return $str . $new_str;
    }
}

if (!function_exists('http_patch_data')) {
    function http_patch_data($url, $token, $data, $methods = 'PATCH')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8",
            "Content-Length: " . strlen(json_encode($data)),
            "authorization:Bearer " . $token,
        ));
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $methods);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        ob_start();
        // echo json_encode($data);
        curl_exec($ch);
        // dd($content);
        // return json_encode($data);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        // dd($return_code);
        if ($return_code == 401 || $return_code == 403) {
            return ['code' => $return_code, 'data' => $return_content];
        }

        if ($return_code == 200) {
            return ['code' => 1, 'data' => $return_content];
        }
        return ['code' => 0, 'data' => $return_content];
    }
}

/**
 * Created by PhpStorm.
 * User: tanteng
 * Date: 2017/7/22
 * Time: 13:48
 */
if (!function_exists('http_post_data')) {
    function http_post_data($url, $token, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8",
            "Content-Length: " . strlen(json_encode($data)),
            "authorization:Bearer " . $token,
        ));
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        // dd($return_content);
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($return_code == 401 || $return_code == 403) {
            return ['code' => $return_code, 'data' => $return_content];
        }

        if ($return_code == 200) {
            return ['code' => 1, 'data' => $return_content];
        }
        return ['code' => 0, 'data' => $return_content];
    }
}

if (!function_exists('http_get_data')) {
    function http_get_data($url, $token)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8",
            "Content-Length: ",
            "authorization:Bearer " . $token,
        ));
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        // dd( $return_content);
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        // dd( $return_code);
        if ($return_code == 401 || $return_code == 403) {
            return ['code' => $return_code, 'data' => $return_content];
        }

        if ($return_code == 200) {
            return ['code' => 1, 'data' => $return_content];
        }
        return ['code' => 0, 'data' => $return_content];
    }
}

if (!function_exists('get_month_count_day')) {
    function get_month_count_day()
    {
        $year = date('Y');
        $month = date('m');
        switch ($month) {
            case 4:
            case 6:
            case 9:
            case 11:
                $days = 30;
                break;
            case 2:
                if ($year % 4 == 0) {
                    if ($year % 100 == 0) {
                        $days = $year % 400 == 0 ? 29 : 28;
                    } else {
                        $days = 29;
                    }
                } else {
                    $days = 28;
                }
                break;
            default:
                $days = 31;
                break;
        }
        return $days;
    }
}

/**
 * @desc 根据两点间的经纬度计算距离
 * @param float $lat 纬度值
 * @param float $lng 经度值
 */
if (!function_exists('get_distance')) {
    /**
     * 计算两组经纬度坐标 之间的距离
     * params ：lat1 纬度1； lng1 经度1； lat2 纬度2； lng2 经度2； len_type （1:m or 2:km);
     * return m or km
     */
    function get_distance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
        //函数里面定义常量
        $EARTH_RADIUS = 6378.137; //地球半径
        $PI = 3.1415926;
        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS;
        $s = round($s * 1000);
        if ($len_type > 1) {
            $s /= 1000;
        }
        return round($s, $decimal);
    }
}

if (!function_exists('format_number')) {
    function format_number($number, $point = 2)
    {
        if (false === ($pointPlace = strpos($number, '.'))) {
            return number_format($number, 2, '.', '');
        } else {
            $tmpArr = explode('.', $number);
            return $tmpArr[0] . '.' . substr($tmpArr[1], 0, $point);
        }
    }
}

/**
 * 两个数之间生成随机小数
 */
if (!function_exists('generate_rand')) {
    function generate_rand($m, $n)
    {
        if ($m > $n) {
            $numMax = $m;
            $numMin = $n;
        } else {
            $numMax = $n;
            $numMin = $m;
        }
        /**
         * 生成$numMin和$numMax之间的随机浮点数，保留2位小数
         */
        $rand = $numMin + mt_rand() / mt_getrandmax() * ($numMax - $numMin);
        return floatval(number_format($rand, 2));
    }
}

/**
 * 验证手机号
 * @param $mobile
 */
if (!function_exists('is_mobile')) {
    function is_mobile($mobile)
    {
        if (!is_numeric($mobile)) {
            return false;
        }
        $pattern = '/^1[3-9]\d{9}$/'; // 正则表达式，匹配手机号  
        return preg_match($pattern, $mobile); // 使用 preg_match 函数进行匹配 
    }
}

/**
 * xml转数组
 * @param xml xml内容
 */
if (!function_exists('xml_to_arr')) {
    function xml_to_arr($xml)
    {
        try {
            $objectxml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $xmljson = json_encode($objectxml); //将对象转换个JSON
            $xmlarray = json_decode($xmljson, true); //将json转换成数组
            return $xmlarray;
        } catch (\Throwable $th) {
            $msg = $th->getMessage();
            file_put_contents('./xmlError.log', $msg);
        }
    }
}
